package com.base.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.homework.service.HomeWorkService;

/**
 * Servlet implementation class FormFileUploadServlet
 */
public class FormFileUploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FormFileUploadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		   DiskFileItemFactory factory = new DiskFileItemFactory(); 
		    //当内存中文件数据达到THRESHOLD_SIZE后，就写入临时文件中，避免上传大文件时，消化太多内存 
		      
		    ServletFileUpload upload = new ServletFileUpload(factory); 
		    upload.setHeaderEncoding("UTF-8"); 
		    upload.setSizeMax(2*1024*1024); 
		 //  String savePath=request.getServletContext().getRealPath("bsea"+File.separator+"img"+File.separator);
		    String savePath="C:/tmp/";
		    String uid="";
		    String hid="";
		    
		    List items;
					try {
						items = upload.parseRequest(request);
						 Iterator iter = items.iterator(); 
						 while(iter.hasNext()){ 
						      FileItem item = (FileItem) iter.next(); 
						      //isFormField().  如果循环里，本次循环的元素的类型是file，那么这个方法返回false, 如果不是文件类型，就返回true
						      if (item.isFormField()) {//普通表单域
						    	  System.out.println("---isFormField=true------"+item.getFieldName()+"----------------"+item.getString());
						    	   if("uid".equals(item.getFieldName())){
						    		   uid=item.getString();
						    	   }
						    	   if("hid".equals(item.getFieldName())){
						    		   hid=item.getString();
						    	   }
						    	  
						      } else { 
						        String fileName = item.getName().replace("/", "\\"); 
						        System.out.println("---fileName------"+fileName+"----------------");
						        int i = fileName.lastIndexOf("\\"); 
						        fileName = fileName.substring(i+1); 
						        //避免重复 
						        fileName = System.currentTimeMillis() + fileName; 
						          //File.separator 在window系统下，表示 \  ，在linux系统下是一个/
						        File uploadedFile = new File( savePath+ fileName); 
						        item.write(uploadedFile); 
						      }
						      
						      
						      
						    }
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					HomeWorkService hs=new HomeWorkService();
					hs.submitHomeWork(uid, hid);
					
					response.sendRedirect("index.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
